<?php
class  Admin_Model_DbTable_Taxonomy  extends  Kieumau_Db_Common
{
	
	public function init(){
		
		parent::init();
		$this->_name = 'taxonomy';
		$this->fieldsPrimary = array('enabled', 'status', 'level', 'create_date', 'modify_date');
		$this->fieldsLanguage = array('name');
			
	}
    public function listGroupUser(){
    	try {

			$sql = "SELECT * FROM `".$this->_name."`";
    		$this->_db->setFetchMode ( Zend_Db::FETCH_ASSOC );
    		$result = $this->_db->fetchAll($sql);		
    		return $result;
			
    	} catch ( Exception $ex ) {
    		echo $ex->getMessage ();
    	}
    }
    
	public function getGroupParentUser($idParent=0){
		
		$sql = "SELECT * FROM `".$this->_name."` WHERE `group_parent` = ?";
		$this->_db->setFetchMode ( Zend_Db::FETCH_ASSOC );
		$result = $this->_db->fetchAll($sql, array($idParent));		
		return $result;
	}
	
	
	// @author: Michael
	// $date: 07/23/2012
	// @function: insertTable
	// @return: id row of table
	public function insertTable($data){
		// Id of table pri
		$idTablePri = 0;
		try{
			// Add data into primary table and return id primary of table
			$dataPrimary = array();
			foreach( $this->fieldsPrimary as $field ){
				if(isset($data[$field])){
					$dataPrimary[$field] = $data[$field];
				}
			}
			
		$this->_db->beginTransaction();//Begin Transaction
			//SET LEVEL FOR NEWS:	Get the final level, then +1 and set into new news
			$sql = 'SELECT level FROM '.$this->_name.' ORDER BY level desc limit 0,1'; 
			$currentLevel = $this->_db->fetchAll($sql);
			if(!empty ($currentLevel[0]['level']) )
				$newLevel = $currentLevel[0]['level'] + 1;
			else
				$newLevel = 1;
			$dataPrimary['level'] 		= $newLevel;
			$dataPrimary['create_date'] = date('Y-m-d H:i:s');
			$dataPrimary['modify_date'] = date('Y-m-d H:i:s');
			$this->_db->insert($this->_name, $dataPrimary);
			$idTablePri 				= $this->_db->lastInsertId();
		$query = $this->_db->commit();//Commit query to database
			
			// Add data language into second table (table_language)
			if($idTablePri>0){
	
				$language = new Kieumau_Db_Language();
				$objLanguages = $language->getLanguages();
				
				$objLanguagesDefault = $language->getLanguageDefault();
	
				foreach( $objLanguages as $objLanguage ){
					$dataLanguage = array();
					$dataLanguage[$this->_name."_id"] = $idTablePri;
					$dataLanguage["lang_id"] = $objLanguage['id'];
					foreach( $this->fieldsLanguage as $field ){
						if(isset($data[$field][$objLanguage['code']])&&$data[$field][$objLanguage['code']]!=""){
							$dataLanguage[$field] = $data[$field][$objLanguage['code']];
						}else{
							$dataLanguage[$field] = $data[$field][$objLanguagesDefault['code']];
						}
					}
				
					$this->insertData($this->_name."_language", $dataLanguage);
				}
			}
				
			return $idTablePri;
				
		}catch(Exception $ex){
			return -1;
		}
	}
	
	
	// @author: Michael.Tran
	// $date: 07/24/2012
	// @function: updateTable
	// @return: id row of table
	public function updateTable($data, $id){
		try{
			if(isset($data['id']))
				unset($data['id']);//Remove id of this data if exists.
	
			// Add data into primary table and return id primary of table
			$dataPrimary = array();
			foreach( $this->fieldsPrimary as $field ){
				if(isset($data[$field])){
					$dataPrimary[$field] = $data[$field];
				}
			}
			$dataPrimary['modify_date'] = Date('Y-m-d H:i:s');
			$this->_db->update($this->_name, $dataPrimary, 'id = '. (int)$id );
			$idTablePri = $id;
			
			// Add data language into second table (table_lang)
			if(count($this->fieldsLanguage)){
				$language = new Kieumau_Db_Language();
				$objLanguages = $language->getLanguages();
				$objLanguagesDefault = $language->getLanguageDefault();
				foreach( $objLanguages as $objLanguage ){
					$dataLanguage = array();
// 					$dataLanguage[$this->_name."_id"] = $idTablePri;
// 					$dataLanguage["lang_id"] = $objLanguage['id'];
					foreach( $this->fieldsLanguage as $field ){
						if(isset($data[$field][$objLanguage['code']])){
							$dataLanguage[$field] = $data[$field][$objLanguage['code']];
						}else{
							$dataLanguage[$field] = $data[$field][$objLanguagesDefault['code']];
						}
					}
					
					/*======================  Debug Part 1 =======================================
					echo "<br/> <hr/> Data: <br/>";
					var_dump($dataLanguage);
					echo "<br/>  Table Name: <br/>";
					echo $this->_name."_language";
					echo "<br/> <br/>WHERE SQL: <br/>";
					echo $this->_name."_id = ".$idTablePri." AND lang_id = '".$objLanguage['id']."'";
					echo "<br/> <hr/> <br/>";
					/*======================  END debug Part 1 =======================================*/
					$this->_db->update($this->_name."_language", $dataLanguage, $this->_name."_id = ".$idTablePri." AND lang_id = '".$objLanguage['id']."'");
				}
			}
			return $id;
		}catch(Exception $ex){
			echo $ex->getMessage();
			exit();
			return -1;
		}
	}
	
	
	public function activate($id){
		try {
			$sql = "SELECT enabled FROM ".$this->_name." WHERE id = ".(int)$id;
			$enabled = $this->_db->fetchAll($sql);
			$enabled = !$enabled[0]['enabled'];
			$this->_db->update($this->_name,array('enabled'=>$enabled), 'id = '.(int)$id);
    	} catch ( Exception $ex ) {
    		echo $ex->getMessage ();
    	}
	}
}
